package Project;
//___________________________IMPORTACIONES______________________________________

import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import java.awt.event.ItemEvent;
import java.sql.*;
import javax.swing.DefaultComboBoxModel;
//______________________________________________________________________________

public class ReporteEspeci extends javax.swing.JDialog {
//_____________________________ATRIBUTOS________________________________________

    private IniciarReporte jasper;
    String base;
    String base2;
    int opcion;
    int ano1;
    int ano2;
    int tiempo;
//_____________________________CONSTRUCTOR______________________________________

    public ReporteEspeci(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        llenarcomobo();
        llenarcomobo2();
        llenarComboVarios();
        jasper = new IniciarReporte();
        this.setLocationRelativeTo(rootPane);
        rdbnAno1.setSelected(true);
        cbxAno1.setEnabled(false);
        cbxAno2.setEnabled(false);
    }
//________________________________METODOS_______________________________________

    private void llenarComboVarios() {
        BaseDeDatos.conectarse();
        try {
            Statement s = BaseDeDatos.con.createStatement();
            ResultSet rs = s.executeQuery("select * from tempo2 where ano < 2035");
            while (rs.next()) {
                cbxAno2.removeAllItems();
                base = rs.getString(1);
                cbxAno1.addItem(base);
                ano1 = Integer.parseInt(cbxAno1.getSelectedItem().toString());
                for (int i = ano1 + 1; i < 2036; i++) {
                    cbxAno2.addItem(i);
                }
                selectItemVarios();
            }
            rs.close();
            s.close();
            BaseDeDatos.desconectarse();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        pack();
        setLocationRelativeTo(null);
    }
//__________________________________________________________________________________    

    private void selectItemVarios() {

        cbxAno1.addItemListener(new java.awt.event.ItemListener() {

            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxAno3ItemStateChanged(evt);
            }

            private void cbxAno3ItemStateChanged(ItemEvent evt) {
                ano1 = Integer.parseInt(cbxAno1.getSelectedItem().toString());
                cbxAno2.removeAllItems();
                for (int i = ano1 + 1; i < 2036; i++) {
                    cbxAno2.addItem(i);
                }
            }
        });
    }
//__________________________________________________________________________________        

    private void llenarcomobo() {
        BaseDeDatos.conectarse();
        try {
            Statement s = BaseDeDatos.con.createStatement();
            ResultSet rs = s.executeQuery("select count(date_part('year',accidente.fecha)) as total, date_part('year',accidente.fecha) as fecha from accidente  group by date_part('year',accidente.fecha) order by fecha asc");
            while (rs.next()) {
                base = rs.getString(2);
                cbxSelAno1.addItem(base);

            }
            rs.close();
            s.close();
            BaseDeDatos.desconectarse();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        pack();
        setLocationRelativeTo(null);
    }

    private void llenarcomobo2() {
        BaseDeDatos.conectarse();
        try {
            Statement s = BaseDeDatos.con.createStatement();
            ResultSet rs = s.executeQuery("select * from tempo2 where ano < 2035");
            while (rs.next()) {
                base2 = rs.getString(1);
                cbxAno1.addItem(base2);
                cbxAno2.addItem(base2);


            }
            rs.close();
            s.close();
            BaseDeDatos.desconectarse();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        pack();
        setLocationRelativeTo(null);
    }
//______________________________________________________________________________

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cbxCategoria = new javax.swing.JComboBox();
        cbxGenerarReporte = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbxSelAno1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cbxFrecuencia = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        lblError = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        rdbnAno1 = new javax.swing.JRadioButton();
        rdbnAno2 = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        cbxAno1 = new javax.swing.JComboBox();
        cbxAno2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Generar Reporte Gráficos de comparación");
        setMinimumSize(new java.awt.Dimension(503, 215));
        setModal(true);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Generar Graficas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(503, 313));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel4.setText("La graficas seran generadas sobre la entidad accidentes.");

        cbxCategoria.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cbxCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Por periodo", "Rutas", "Perdidas Materiales por marca" }));
        cbxCategoria.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        cbxCategoria.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxCategoriaItemStateChanged(evt);
            }
        });

        cbxGenerarReporte.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        cbxGenerarReporte.setText("Generar  Grafica");
        cbxGenerarReporte.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        cbxGenerarReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxGenerarReporteActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("Categoria:");

        cbxSelAno1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cbxSelAno1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "------------" }));
        cbxSelAno1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setText("Seleccione el año:");

        cbxFrecuencia.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cbxFrecuencia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Trimestral", "Semestral" }));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setText("Frecuencia :");

        setVisible(false);
        lblError.setForeground(new java.awt.Color(204, 0, 0));

        jLabel5.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel5.setText("Cantidad de años:");

        buttonGroup1.add(rdbnAno1);
        rdbnAno1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        rdbnAno1.setText("Uno");
        rdbnAno1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rdbnAno1ItemStateChanged(evt);
            }
        });

        buttonGroup1.add(rdbnAno2);
        rdbnAno2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        rdbnAno2.setText("Varios");
        rdbnAno2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rdbnAno2ItemStateChanged(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel6.setText("Rango de años:");

        cbxAno1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        cbxAno2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbxSelAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbxCategoria, 0, 216, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbxFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(88, 88, 88)
                                .addComponent(cbxGenerarReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rdbnAno1)
                                .addGap(18, 18, 18)
                                .addComponent(rdbnAno2))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(26, 26, 26)
                                .addComponent(cbxAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbxAno2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 107, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(rdbnAno1)
                    .addComponent(rdbnAno2))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbxAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxAno2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxSelAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxGenerarReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
//______________________________________________________________________________

private void cbxGenerarReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxGenerarReporteActionPerformed

    opcion = cbxCategoria.getSelectedIndex();

    tiempo = cbxFrecuencia.getSelectedIndex();
    // System.out.println("año 1: "+ano1+ " "+"año 2: "+ ano2);

    if (rdbnAno1.isSelected() == true) {
        if (cbxSelAno1.getSelectedIndex() == 0) {
            lblError.setText("No ha seleccionado el año");
            lblError.setVisible(true);


        } else {
            lblError.setVisible(false);
            lblError.setText("");
            ano1 = Integer.parseInt(cbxSelAno1.getSelectedItem().toString());
            if (opcion == 0) {

                switch (tiempo) {
                    case 0:
                        jasper.ejecutarReporteEspecif(ano1, "Accid. Trimestral", "ReportEspeciPerioTri");
                        break;
                    case 1:
                        // this.dispose();
                        jasper.ejecutarReporteEspecif(ano1, "Accid. Semestral", "ReportEspeciPerio");

                }
            } else if (opcion == 1) {
                switch (tiempo) {
                    case 0:
                        jasper.ejecutarReporteEspecif(ano1, "Accid. Trimestral", "ReportEspeciPerioRutTri");
                        break;
                    case 1:
                        // this.dispose();
                        jasper.ejecutarReporteEspecif(ano1, "Accid. Semestral", "ReportEspeciPerioRut");

                }


            } else if (opcion == 2) {
                jasper.ejecutarReporteEspecif(ano1, "Accid. Por Grados", "ReportEspeciPerioMarc");

            }
        }
    }
    if (rdbnAno2.isSelected() == true) {

        lblError.setVisible(false);
        lblError.setText("");
        ano1 = Integer.parseInt(cbxAno1.getSelectedItem().toString());
        ano2 = Integer.parseInt(cbxAno2.getSelectedItem().toString());

        if (opcion == 0) {

            switch (tiempo) {
                case 0:
                    jasper.ejecutarReporteEspecifVarios(ano1, ano2, "Accid. Periodo Trimestral", "ReportEspeciPerioGenVarTrimest");
                    break;
                case 1:
                    jasper.ejecutarReporteEspecifVarios(ano1, ano2, "Accid. Periodo Semestral", "ReportEspeciPerioGenVarSemest");
                    break;
                case 2:
                    jasper.ejecutarReporteEspecifVarios(ano1, ano2, "Accid. Periodo Mensual", "ReportEspeciPerioGenMens");
                    break;
            }       
            
        } else if (opcion == 1) {
            switch (tiempo) {
                case 0:
                    jasper.ejecutarReporteEspecifVarios(ano1, ano2, "Accidentes por Rutas Trimestral", "ReportEspeciPerioRutVarTrimest");
                    break;
                case 1:
                    jasper.ejecutarReporteEspecifVarios(ano1, ano2, "Accidentes por Rutas Semestral", "ReportEspeciPerioRutVarSemest");
                    break;

                case 2:
                    jasper.ejecutarReporteEspecifVarios(ano1, ano2, "Accidentes por Rutas Mensual", "ReportEspeciPerioRutMens");
                    break;

            }


        } else if (opcion == 2) {
            jasper.ejecutarReporteEspecif(ano1, "Accid. Por Grados", "");

        }
    }

}//GEN-LAST:event_cbxGenerarReporteActionPerformed

    private void cbxCategoriaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxCategoriaItemStateChanged
        if (cbxCategoria.getSelectedIndex() == 2 && rdbnAno2.isSelected() != true) {
            cbxFrecuencia.setEnabled(false);
        } else {
            cbxFrecuencia.setEnabled(true);
        }
    }//GEN-LAST:event_cbxCategoriaItemStateChanged

    private void rdbnAno1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rdbnAno1ItemStateChanged
        if (cbxCategoria.getItemCount() < 3) {
            DefaultComboBoxModel mod = new DefaultComboBoxModel();
            mod.addElement("Perdidas Materiales Por Marca");
            cbxCategoria.addItem(mod.getElementAt(0));
            cbxAno1.setEnabled(false);
            cbxAno2.setEnabled(false);
            cbxSelAno1.setEnabled(true);
            cbxCategoria.setSelectedIndex(0);
        }

        if (cbxFrecuencia.getItemCount() == 3) {
            cbxFrecuencia.removeItemAt(2);
            cbxAno1.setEnabled(false);
            cbxAno2.setEnabled(false);
            cbxSelAno1.setEnabled(true);
            cbxCategoria.setSelectedIndex(0);
        } else {
            cbxAno1.setEnabled(false);
            cbxAno2.setEnabled(false);
            cbxSelAno1.setEnabled(true);
            cbxCategoria.setSelectedIndex(0);

        }
    }//GEN-LAST:event_rdbnAno1ItemStateChanged

    private void rdbnAno2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rdbnAno2ItemStateChanged
        if (cbxFrecuencia.getItemCount() < 3) {
            DefaultComboBoxModel mod = new DefaultComboBoxModel();
            mod.addElement("Mensual");
            cbxFrecuencia.addItem(mod.getElementAt(0));
        }

        if (cbxCategoria.getItemCount() == 3) {
            cbxCategoria.removeItemAt(2);
        }
        cbxAno1.setEnabled(true);
        cbxAno2.setEnabled(true);
        cbxSelAno1.setEnabled(false);
        cbxCategoria.setSelectedIndex(0);


    }//GEN-LAST:event_rdbnAno2ItemStateChanged
//______________________________________________________________________________
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbxAno1;
    private javax.swing.JComboBox cbxAno2;
    private javax.swing.JComboBox cbxCategoria;
    private javax.swing.JComboBox cbxFrecuencia;
    private javax.swing.JButton cbxGenerarReporte;
    private javax.swing.JComboBox cbxSelAno1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblError;
    private javax.swing.JRadioButton rdbnAno1;
    private javax.swing.JRadioButton rdbnAno2;
    // End of variables declaration//GEN-END:variables
}
